﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BBD_DAL.Entities;
using BBD_DAL.DBConnector;
using System.Data;
using MySql.Data.MySqlClient;

namespace BBD_DAL.DAO
{
	public class AnswerDao : IBaseDao<Answer>
	{

		private DBConnection dbConnector;
		private Dictionary<string, string> queries;

		public AnswerDao(string connectionString)
		{
			this.Initialize(connectionString);
		}

		public void Initialize(string connectionString)
		{
			this.dbConnector = new DBConnection(connectionString);
			queries = new Dictionary<string, string>();
			queries.Add("Get", "SELECT * FROM odpowiedzi WHERE odpowiedz_id = @id;");
			queries.Add("GetByQuestionnaire", "SELECT * FROM odpowiedzi WHERE ankieta_id = @id;");
			queries.Add("Save", "UPDATE odpowiedzi SET pytanie_id = @pid, odpowiedz = @answer, ankieta_id = @aid WHERE odpowiedz_id = @id;");
			queries.Add("Insert", "INSERT INTO odpowiedzi (pytanie_id, ankieta_id, odpowiedz) VALUES (@pid, @aid, @answer);");
			queries.Add("Delete", "DELETE FROM odpowiedzi WHERE id = @id;");
			queries.Add("DeleteAll", "DELETE FROM odpowiedzi; ALTER TABLE odpowiedzi AUTO_INCREMENT = 1;");
			queries.Add("List", "SELECT * FROM odpowiedzi;");
		}

		public Answer Get(long id)
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["Get"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			}, out data);

			List<Answer> answers = this.Convert(data);

			return answers != null ? answers[0] : null;
		}
		public List<Answer> GetByQuestionnaire(long id)
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["GetByQuestionnaire"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
				
			}, out data);

			List<Answer> answers = this.Convert(data);

			return answers != null ? answers : null;
		}


		public void Save(Answer entity)
		{
			this.dbConnector.RunQuery(this.queries["Save"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = entity.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@pid", Value = entity.Question.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@aid", Value = entity.Questionnaire.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@answer", Value = entity.GivenAnswer, MySqlDbType = MySqlDbType.Bit}
			});
		}

		public Answer Insert(Answer entity)
		{
			long id;
			this.dbConnector.RunQuery(this.queries["Insert"], new MySqlParameter[]{				
				new MySqlParameter(){ParameterName = "@pid", Value = entity.Question.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@aid", Value = entity.Questionnaire.Id, MySqlDbType = MySqlDbType.Int64},
				new MySqlParameter(){ParameterName = "@answer", Value = entity.GivenAnswer, MySqlDbType = MySqlDbType.Bit}
			}, out id);

			entity.Id = id;

			return entity;
		}

		public void Delete(long id)
		{
			this.dbConnector.RunQuery(this.queries["Delete"], new MySqlParameter[]{
				new MySqlParameter(){ParameterName = "@id", Value = id, MySqlDbType = MySqlDbType.Int64}
			});
		}

		public void Delete(Answer entity)
		{
			this.Delete(entity.Id);
		}

		public void Delete()
		{
			this.dbConnector.RunQuery(this.queries["DeleteAll"], null);
		}

		public List<Answer> List()
		{
			DataTable data;
			this.dbConnector.RunQuery(this.queries["List"], null, out data);
			return this.Convert(data);
		}

		private List<Answer> Convert(DataTable data)
		{
			if (data == null || data.Rows == null || data.Rows.Count == 0)
				return null;

			List<Answer> answers = new List<Answer>();
			foreach (DataRow row in data.Rows)
			{
				Answer answer = new Answer();
				answer.Id = Int64.Parse(row["odpowiedz_id"].ToString());
				//answer.GivenAnswer = (bool)row["odpowiedz"];
				answer.GivenAnswer = System.DBNull.Value.Equals(row["odpowiedz"]) ? (bool?)null : (bool?)row["odpowiedz"];// Int16.Parse(row["wiek"].ToString());
				answer.Questionnaire = new Questionnaire() { Id = Int64.Parse(row["ankieta_id"].ToString()) };
				answer.Question = new Question() { Id = Int64.Parse(row["pytanie_id"].ToString()) };
				answers.Add(answer);
			}
			return answers;
		}
	}
}
